#include <windows.h>
#include "resource.h"
#include "hWndDec.h"

//#include "algoritm.h"

const char g_szClassName[] = "Arbori_B";
char matrice[5][3];
HWND dlg=NULL;
// procedura dialog Help->About
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){
	
	switch(Message){
		case WM_INITDIALOG:
			return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wParam)){
			case IDOK1:
				EndDialog(hwnd, IDOK1);
				break;
			}
			break;
		case WM_CLOSE:
			EndDialog(hwnd,WM_CLOSE);
			default:	return FALSE;
				
	}
	return TRUE;
}
// procedura dialog File->Add
BOOL CALLBACK AddDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){
	
	switch(Message){
		case WM_INITDIALOG:
			return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wParam)){

				case IDOK2:
					GetDlgItemText(hwnd, IDC_EDIT1 , matrice[0], 3);
					GetDlgItemText(hwnd, IDC_EDIT2 , matrice[1], 3);
					GetDlgItemText(hwnd, IDC_EDIT3 , matrice[2], 3);
					GetDlgItemText(hwnd, IDC_EDIT4 , matrice[3], 3);
					GetDlgItemText(hwnd, IDC_EDIT5 , matrice[4], 3);
					ShowWindow(hwnd,SW_PARENTOPENING);
					ShowWindow(hwnd,SW_NORMAL);
					EndDialog(dlg,NULL);
					break;
			}
			break;
		case WM_CLOSE:
			EndDialog(hwnd,WM_CLOSE);
			break;
			default:	return FALSE;
				
	}
	return TRUE;
}
// Step 4: the Window Procedurea
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT paint;
	HDC hDC;
	HBRUSH NewBrush = CreateSolidBrush(RGB(200, 200, 200 ));
	COLORREF textcolor =  RGB( 200 , 200 , 200);
	COLORREF white = RGB( 200 , 200 , 200);//RGB(255,255,255);
	static HWND hWndEdit;
	char numar[3];
	int nr;

    switch(msg)
    {
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_ADD:
					dlg = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG2), hwnd , AddDlgProc);
					ShowWindow(dlg,SW_NORMAL);
					break;
				case ID_FILE_EXIT:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
					break;

				case ID_ABOUT_DEMONSTRATOR:
					{
					int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AboutDlgProc);
					break;
					}
				case BN_CLICKED:
					if(hCloseBTN == (HWND)lParam) DestroyWindow(hwnd);
					/*if(hInsertBTN == (HWND)lParam){ 
						GetDlgItemText(hWndEdit, 7, numar, sizeof(numar));
						nr = atoi(numar);
					if(hDeleteBTN == (HWND)lParam) {
						GetDlgItemText(hWndEdit, 7, numar, sizeof(numar));
						nr = atoi(numar);
						}
					if(hSearchBTN == (HWND)lParam) {
						GetDlgItemText(hWndEdit, 7, numar, sizeof(numar));
						nr = atoi(numar);
						}
						*/
					break;
			}
			break;
        case WM_CLOSE:
			int iResult;
			iResult = MessageBox(hwnd, "Doriti sa inchideti aplicatia?" ,"Atentie!", MB_OKCANCEL | MB_ICONQUESTION);
				if(iResult == IDOK) 	DestroyWindow(hwnd);
					else if(iResult == IDCANCEL)	return false;
			break;
		case WM_CREATE:
			{
			hDeleteBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Delete", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
635, 265, 80, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
			hInsertBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Insert", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 720, 265, 80, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
			hSearchBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Search", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 805, 265, 80, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
			hCloseBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Close", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 890, 265, 80, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
			// Create an edit box to type messages
			hWndEdit = CreateWindow( "EDIT", "" , WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL|ES_AUTOVSCROLL|ES_CENTER|ES_NUMBER, 550, 265, 80, 30, hwnd, (HMENU) 7 , NULL , NULL);
			break;
			}
		case WM_PAINT:
			hDC = BeginPaint(hwnd, &paint);
			//SelectObject( hDC, NewBrush);
			RoundRect(hDC, 15, 10, 970, 260, 5 , 5);
			SelectObject( hDC, NewBrush);
			RoundRect(hDC, 470, 20, 530, 43 , 5, 5);
			SetBkColor( hDC, textcolor );
			TextOut(hDC, 473, 23, " 5     65", 9);
			DeleteObject(NewBrush);
			RoundRect(hDC, 251, 50, 302, 73, 5, 5);
			SetBkColor( hDC, textcolor );
			TextOut(hDC, 253, 53, " 1    65 ", 9);
			DeleteObject(NewBrush);
			RoundRect(hDC, 700, 50, 751, 73, 5, 5);
			SetBkColor( hDC, textcolor );
			TextOut(hDC, 701, 54, " 6    66 ", 9);
			DeleteObject(NewBrush);
			RoundRect(hDC, 100, 122, 155, 96, 5, 5);
			SetBkColor( hDC, textcolor );
			TextOut(hDC, 104, 104, " 6    66 ", 9);
			DeleteObject(NewBrush);
			
			
			if( strcmp(matrice[0],"")!=0 ){

				RoundRect(hDC, 100, 300, 120, 320, 5 , 5);
				SetBkColor( hDC, white );
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 104, 302, matrice[0] , strlen(matrice[1]));

				RoundRect(hDC, 120, 300, 140, 320 , 5, 5);
				SetBkColor( hDC, white );
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 124, 302, matrice[1], strlen(matrice[1]));

				RoundRect(hDC, 140, 300, 160, 320, 5 , 5);
				SetBkColor( hDC, white );
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 144, 302, matrice[2], strlen(matrice[2]));

				RoundRect(hDC, 160, 300, 180, 320, 5 , 5);
				SetBkColor( hDC, white );
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 164, 302, matrice[3], strlen(matrice[3]));

				RoundRect(hDC, 180, 300, 200, 320, 5 , 5);
				SetBkColor( hDC, white );
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 184, 302, matrice[4], strlen(matrice[4]));
				
			}
			
			if( strcmp(matrice[0],"")!=0 ){

				SelectObject( hDC, NewBrush);
				MoveToEx(hDC, 500, 43, NULL);
				LineTo(hDC, 214, 122);
				RoundRect(hDC, 64, 122, 364, 145, 5, 5);
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 74, 126, "Acest program va merge intro buna zi!!!!", 37);
				DeleteObject(NewBrush);

				SelectObject( hDC, NewBrush);
				MoveToEx(hDC, 500, 43, NULL);
				LineTo(hDC, 814, 122);
				RoundRect(hDC, 664, 122, 964, 145, 5, 5);
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 674, 126, "Acest program va merge intro buna zi!!!!", 37);
				DeleteObject(NewBrush);

				SelectObject( hDC, NewBrush);
				MoveToEx(hDC, 500, 43, NULL);
				LineTo(hDC, 314, 222);
				RoundRect(hDC, 164, 222, 464, 245, 5, 5);
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 174, 226, "Acest program va merge intro buna zi!!!!", 37);
				DeleteObject(NewBrush);

				SelectObject( hDC, NewBrush);
				MoveToEx(hDC, 500, 43, NULL);
				LineTo(hDC, 714, 222);
				RoundRect(hDC, 564, 222, 864, 245, 5, 5);
				SetBkColor( hDC, textcolor );
				TextOut(hDC, 574, 226, "Acest program va merge intro buna zi!!!!", 37);
				DeleteObject(NewBrush);
			}
			//DeleteObject(NewBrush);
			EndPaint(hwnd, &paint);
			break;
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
			break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
	HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON, 16, 16, 0);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
						g_szClassName,
						"Demonstrator Grafic Arbori B",
						WS_OVERLAPPEDWINDOW,
						200, 
						50, 
						1000, 365,
						NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, SW_NORMAL);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;

}